import { useCacheStore } from '@/stores/cache';
/* 数据转换相关函数 */
export default {
	ymd (text: string) {
		const arr = text.toString().split('-')
		let str = arr[0] + '年'
		arr[1] && (str += arr[1] + '月')
		arr[2] && (str += arr[2] + '日')
		return str
	},
	number (text: number, length: number = 2) {
		if(isNaN(text)) return ''
    return length === -1 ? parseFloat(text) : parseFloat((text*1).toFixed(length))
  },
	// 行政区域
	region (value: string | number = '') {
		if(!value){
			return ''
		}
		const tree = useCacheStore().regions
		const a = String(value).match(/\d{2}/g)
		let label = tree[a[0]][0]
		if (a[1] !== '00' && !['11','12','31','50'].includes(a[0])) {
			label += tree[a[0]][1][a[1]][0]
		}
		if (a[2] !== '00') {
			label += tree[a[0]][1][a[1]][1][a[2]][0]
		}
		return label
	},
	// 城市
	city (value: string = '', full: boolean = false) {
		if(!value){
			return ''
		}
		const tree = useCacheStore().citys
		const a = String(value).match(/\d{2}/g)
		const index1 = tree.findIndex((r:any)=>r.value==a[0].padEnd(6, '0'))
		// console.log(index1, a[0].padEnd(6, '0'))
		if(a[1] === '00' || ['11','12','31','50'].includes(a[0])){
			return tree[index1]?.label
		}
		const index2 = tree[index1]?.children.findIndex((r:any)=>r.value==(a[0] + a[1]).padEnd(6, '0'))
		if(full){
			return tree[index1]?.label + tree[index1]?.children[index2]?.label
		} else {
			return tree[index1]?.children[index2]?.label
		}
	},
	// 分类
	cate (value: string = '', field: string = 'name') {
		if(!value){
			return ''
		}
		const tree = useCacheStore().cates
		const a = String(value).match(/\d{2}/g)
		const prov = tree[a[0]]
		if (a[1] == '00') {
			return prov[0]
		}
		const city = prov[1][a[1]]
		if (a[2] == '00') {
			return city[0]
		}
		return city[1][a[2]][0]
	},
	// radio, chekbox, select等的值
  label (text: string, options: any) {
    if (options && options[0] && typeof (options[0]) === 'string') {
      return options[text]
    } else if (options && options[text]) {
      return options[text].label
    } else {
      return ''
    }
  },
  empty (text: string, $empty: string = '空') {
    return text ? text : $empty
  },
  // 数组转换为option数组参数, radio, chekbox, select等会用到
  options (text: any) {
    let data = []
    if(typeof (text) === 'string' && text !== ''){
      data = JSON.parse(text)
    } else if (typeof (text) === 'object') {
      data = text
    }
    const options = []
    for (const i in data) {
      if (['string', 'number'].includes(typeof (data[i]))) {
        options.push({ label: data[i] , value: i + '' })
      } else {
        options.push(data[i])
      }
    }
    return options
  },
	// 手机格式化
	mobile (text: string) {
		return text.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
	},
	tabs (text: any, attr: string = 'name') {
    let data = []
    if(typeof (text) === 'string' && text !== ''){
      data = JSON.parse(text)
    } else if (typeof (text) === 'object') {
      data = text
    }
    const tabs = []
		for(const i in data){
      if (typeof (data[i]) === 'string' || typeof (data[i]) === 'number') {
        tabs.push({ [attr]: data[i], value: parseInt(i) })
      } else {
        tabs.push(data[i])
      }			
		}
		return tabs
	},
	money (text: string) {//金额格式化
		return parseFloat(text).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	}
}
